package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qf.dao.MemberDao;
import com.qf.dao.OrderDao;
import com.qf.dao.ReportDao;
import com.qf.service.ReportService;
import com.qf.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouhao
 * @create 2021-12-27 17:20
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    //获取营运数据统计
    @Override
    public Map<String, Object> getBusinessReportData() {

        try {
            //获取当前日期
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            //获得本周一的日期
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获取本月的第一天
            String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            //今日新增会员数
            Integer todayNewMember = memberDao.findMemberCountByDate(today);
            //总会员数
            Integer totalMember = memberDao.findMemberTotalCount();
            //本周新增会员数
            Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
            //本月新增会员数
            Integer thisMonthNewMember = memberDao.findMemberCountAfterDateOnMonth(firstDay4ThisMonth);
            //今日预约数
            Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
            //本周预约数
            Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
            //本月预约数
            Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDateOnMonth(firstDay4ThisMonth);
            //今日到诊数
            Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
            //本周到诊数
            Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
            //本月到诊数
            Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDateOnMonth(firstDay4ThisMonth);
            //热门套餐（取前3）
            List<Map> hotSetmeal = orderDao.findHotSetmeal();

            HashMap<String, Object> map = new HashMap<>();
            map.put("reportDate", today);
            map.put("todayNewMember", todayNewMember);
            map.put("totalMember", totalMember);
            map.put("thisWeekNewMember", thisWeekNewMember);
            map.put("thisMonthNewMember", thisMonthNewMember);
            map.put("todayOrderNumber", todayOrderNumber);
            map.put("thisWeekOrderNumber", thisWeekOrderNumber);
            map.put("thisMonthOrderNumber", thisMonthOrderNumber);
            map.put("todayVisitsNumber", todayVisitsNumber);
            map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
            map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
            map.put("hotSetmeal", hotSetmeal);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
